<?php

/**
 * Form Date Validator class, verify the format of a date : dd/mm/yyyy
 *
 * @author Mathieu Pheulpin - sokaru91@gmail.com
 *
 */
class Mac_Form_Validator_Date extends Mac_Form_Validator_Abstract
{
    private $errors = array(
        'format' => 'La date est au mauvais format'
    );

    public function isValid($value, array $context)
    {
        $matches = array();
        if (preg_match('#^((0?\d)|(\d{2}))/((0?\d)|(\d{2}))/(\d{4})$#', $value, $matches)) {
            $day = sprintf('%02d', $matches[1]);
            $month = sprintf('%02d', $matches[4]);
            $year = sprintf('%04d', $matches[7]);
            if (checkdate($month, $day, $year)) {
                return true;
            }
        }
        $this->setMessage($this->errors['format']);
        return false;
    }
}